package org.pisces.wechat.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.pisces.wechat.commons.aes.AesException;
import org.pisces.wechat.commons.aes.WXBizMsgCrypt;
import org.pisces.wechat.commons.exception.GlobalException;
import org.pisces.wechat.commons.utils.SerializeXmlUtil;
import org.pisces.wechat.enums.EMsgType;
import org.pisces.wechat.mapper.InputMessageMapper;
import org.pisces.wechat.po.Article;
import org.pisces.wechat.po.ImgOutputMessage;
import org.pisces.wechat.po.InputMessage;
import org.pisces.wechat.po.NewsOutputMessage;
import org.pisces.wechat.po.OutputMessage;
import org.pisces.wechat.po.TextOutputMessage;
import org.pisces.wechat.service.api.IUserService;
import org.pisces.wechat.service.api.IWechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thoughtworks.xstream.XStream;

@Service
@Transactional
public class WechatService extends BaseService implements IWechatService {

	@Autowired
	private InputMessageMapper inputMessageMapper;

	@Autowired
	private IUserService userService;

	@Override
	public String advanced(String msgSignature, String timestamp, String nonce, String encyptMsg) {

		try {
			WXBizMsgCrypt pc = new WXBizMsgCrypt();
			String result = pc.DecryptMsg(msgSignature, timestamp, nonce, encyptMsg);
			logger.info(result);
			XStream xs = SerializeXmlUtil.createXstream();
			xs.processAnnotations(InputMessage.class);
			xs.alias("xml", InputMessage.class);
			//Object fromXML = xs.fromXML(result);
			InputMessage inputMessage = (InputMessage) xs.fromXML(result);

			String servername = inputMessage.getToUserName();// 服务端
			String custermname = inputMessage.getFromUserName();// 客户端
			long createTime = inputMessage.getCreateTime();// 接收时间
			Long returnTime = Calendar.getInstance().getTimeInMillis() / 1000;// 返回时间

			switch (inputMessage.getMsgType()) {
			case EVENT:
				switch (inputMessage.getEvent()) {
				case CLICK:
					break;
				case LOCATION:
					logger.info("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
					break;
				case SUBSCRIBE:
					//  添加用户
					userService.subscribe(inputMessage.getFromUserName());
					TextOutputMessage textOutputMessage = new TextOutputMessage();
					textOutputMessage.setContent("欢迎关注\n<a href=\"http://www.baidu.com\">点击了解更多222</a>");
					textOutputMessage.setFromUserName(inputMessage.getToUserName());
					textOutputMessage.setToUserName(inputMessage.getFromUserName());
					textOutputMessage.setCreateTime(Calendar.getInstance().getTimeInMillis() / 1000);
					textOutputMessage.setMsgType(EMsgType.TEXT.getValue());
					processClass(xs, textOutputMessage);
					return xs.toXML(textOutputMessage);
				case UNSUBSCRIBE:
					break;
				case VIEW:
					break;

				default:
					break;
				}
				break;
			case IMAGE:
				ImgOutputMessage imgOutputMessage = new ImgOutputMessage();
				imgOutputMessage.setMediaId("1");
				imgOutputMessage.setFromUserName(inputMessage.getToUserName());
				imgOutputMessage.setToUserName(inputMessage.getFromUserName());
				imgOutputMessage.setCreateTime(createTime);
				imgOutputMessage.setMsgType(EMsgType.IMAGE.getValue());
				processClass(xs, imgOutputMessage);
				return xs.toXML(imgOutputMessage);
			

			case LINK:

			case MUSIC:
			case TEXT:
				TextOutputMessage textOutputMessage = new TextOutputMessage();
				textOutputMessage.setContent("有问题?<a href=\"https://jq.qq.com/?_wv=1027&k=5V32KVb\">点击加入官方QQ群>></a>\n\n您可以在懒熊斗地主官方问题反馈群内向客服反馈bug，经官方证实后"
						+ "，会视情况给予玩家奖励(钻石、道具等)。\n官方群号码：<a href=\"https://jq.qq.com/?_wv=1027&k=5V32KVb\">195030980</a>\n期待您的加入");
				textOutputMessage.setFromUserName(inputMessage.getToUserName());
				textOutputMessage.setToUserName(inputMessage.getFromUserName());
				textOutputMessage.setCreateTime(createTime);
				textOutputMessage.setMsgType(EMsgType.TEXT.getValue());
				processClass(xs, textOutputMessage);
				return xs.toXML(textOutputMessage);
			case VIDEO:
			case VOICE:
				OutputMessage outputMessage = getOutputMessage(servername, custermname, createTime, "sadasdas");
				processClass(xs, outputMessage);
				return xs.toXML(outputMessage);

			case LOCATION:
			default:
				break;
			}

			inputMessageMapper.insertSelective(inputMessage);
			return "";

		} catch (AesException e) {
			logger.error(ExceptionUtils.getStackTrace(e));
			throw new GlobalException("");
		}

	}

	private OutputMessage getOutputMessage(String servername, String custermname, Long returnTime, String content) {
		NewsOutputMessage newsOutputMessage = new NewsOutputMessage();
		newsOutputMessage.setFromUserName(servername);
		newsOutputMessage.setToUserName(custermname);
		newsOutputMessage.setCreateTime( Calendar.getInstance().getTimeInMillis() / 1000);
		newsOutputMessage.setMsgType(EMsgType.NEWS.getValue());
		List<Article> articels = new ArrayList<>();
		Article redPackArticle = new Article();
		redPackArticle.setDescription("邀请新人送合成炉");
		redPackArticle.setPicUrl("http://cdn.lan-x.cn/wechat/image/hcl.jpg");
		redPackArticle.setTitle("邀请新人送合成炉");
		redPackArticle.setUrl("https://mp.weixin.qq.com/s/w2SBWoxZ66w3QNu9IMBXWA");
		articels.add(redPackArticle);
//		Article article = new Article();
//		article.setDescription("祝贺懒熊斗地主与2463网游服务平台战略合作，福利继续！");
//		article.setPicUrl("http://cdn.lan-x.cn/image/system/share/gsd.png");
//		article.setTitle("祝贺懒熊斗地主与2463网游服务平台战略合作，福利继续！");
//		article.setUrl("https://mp.weixin.qq.com/s/d57ufUCIEmSVs4ElZbdJwQ");
//		articels.add(article);
		newsOutputMessage.setArticles(articels);
		newsOutputMessage.setArticleCount("1");
		return newsOutputMessage;
	}

	private void processClass(XStream xs, OutputMessage outputMessage) {
		Class<? extends OutputMessage> clazz = outputMessage.getClass();
		System.out.println(clazz);
	 if (outputMessage instanceof OutputMessage) {
			xs.processAnnotations(clazz);
		}

	}
	
	
	public static void main(String[] args) {
		XStream xs = SerializeXmlUtil.createXstream();
		NewsOutputMessage textOutputMessage = new NewsOutputMessage();
		textOutputMessage.setFromUserName("111");
		textOutputMessage.setToUserName("222");
		textOutputMessage.setCreateTime( Calendar.getInstance().getTimeInMillis() / 1000);
		textOutputMessage.setMsgType(EMsgType.NEWS.getValue());
		List<Article> articels = new ArrayList<>();
		Article article = new Article();
		article.setDescription("玩3、10、20局斗地主即可前往任务中心领取芝麻开门一个，每日最多领3个~");
		article.setPicUrl("http://cdn.lan-x.cn/wechat/image/IMG_2190.JPG");
		article.setTitle("现金奖励、攻守有道");
		article.setUrl("https://mp.weixin.qq.com/s/-naHPNr77mnk66v3yhjp0A");
		articels.add(article);
		textOutputMessage.setArticles(articels);
		textOutputMessage.setArticleCount("1");
		xs.processAnnotations(NewsOutputMessage.class);
		System.out.println(xs.toXML(textOutputMessage));
	}

}
